//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using SFWK.Core;

namespace SFWK.Core.TraceAdapter
{
    /// <summary>
    /// Provide Framework Logging tool access, adapted(wrapped) to SFWK framework logging interface
    /// </summary>
    public class DefaultTraceAdapter : ILogger
    {
        private static System.Diagnostics.TraceSource _traceSource;
        private static System.Diagnostics.TraceSource _tracer
        {
            get
            {
                if (_traceSource == null)
                {
                    _traceSource = new System.Diagnostics.TraceSource("DefaultTraceAdapter");
                }
                return _traceSource;
            }
        }


        #region ILogger Members

        /// <summary>
        /// Logging method
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="severity"></param>
        public void Log(object source, string title, string message, ELogSeverity severity)
        {
            System.Diagnostics.Trace.WriteLine(
                String.Format("{0}: {1}: {2}: {3}",
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                                source == null ? "null" : source.GetType().ToString(),
                                title,
                                message)
                , severity.ToString());

        }

        /// <summary>
        /// Logging method
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="exception"></param>
        /// <param name="severity"></param>
        public void Log(object source, string title, Exception exception, ELogSeverity severity)
        {
            string exceptionContent = "";
            if (exception != null)
            {
                exceptionContent = exception.Message + Environment.NewLine +
                exception.Source + Environment.NewLine +
                exception.StackTrace;
                if (exception.InnerException != null)
                {
                    exceptionContent = "INNER EXCPETION" + Environment.NewLine +
                    exception.InnerException.Message + Environment.NewLine +
                    exception.InnerException.Source + Environment.NewLine +
                    exception.InnerException.StackTrace;
                }
            }
            System.Diagnostics.Trace.WriteLine(
                String.Format("{0}: {1}: {2}: {3}",
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                                source.GetType().ToString(),
                                title,
                                exceptionContent)
                , severity.ToString());
        }



        public void Log(object source, string title, string message, Exception exception, ELogSeverity severity)
        {
            string exceptionContent = "";
            if (exception != null)
            {
                exceptionContent = exception.Message + Environment.NewLine +
                exception.Source + Environment.NewLine +
                exception.StackTrace;
                if (exception.InnerException != null)
                {
                    exceptionContent = "INNER EXCPETION" + Environment.NewLine +
                    exception.InnerException.Message + Environment.NewLine +
                    exception.InnerException.Source + Environment.NewLine +
                    exception.InnerException.StackTrace;
                }
            }
            System.Diagnostics.Trace.WriteLine(
                String.Format("{0}: {1}: {2}: {3}",
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                                source.GetType().ToString(),
                                title,
                                message,
                                exceptionContent)
                , severity.ToString());
        }

        #endregion
    }
}
